<?php
/**
 * 为了ajxa做本域名的api
 * @todo 能否使用开放api，用jsonp调用。
 */
class ApiController extends BaseController
{
    public function __construct($uri)
    {
        parent::__construct($uri);
        $this->output['http']['content-type'] = 'application/json';
    }

    private function getMe()
    {
        if(isset($_COOKIE['access_token'])) {
            $m = new UserModel();
            $token = $m->decryptToken($_COOKIE['access_token']); //todo move to __construct
            $user = $m->get($token['user_id']);
            $this->output['data'] = $user;
//todo            $this->output['http']['Cache-Control: private'] = 400;
        } else {
            $this->output['http']['status_code'] = 400;
        }
        return $this->output;
    }

    public function route()
    {
        if(1 === preg_match('/^\/api\/me$/', $this->uri)) {
            switch($this->http_method) {
                case 'get' :
                    return $this->getMe();
                    break;
            }
        }
        throw new ControllerException();
    }
}
?>
